<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">角色管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a><cite>系统管理</cite></a>
          <a><cite>角色管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            <form id="searchForm" onsubmit="return false;">
                名称：<input name="name" class="layui-input search-input" type="text" placeholder="输入名称"/>&emsp;
                状态：<select name="status" lay-verify="">
                    <option value="">请选择一个状态</option>
                    <option value="0">正常</option>
                    <option value="-1">禁用</option>
                </select>
                <button id="role-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            </form>
        </div>

        <script type="text/html" id="role-toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</button>
                <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="operate"><i class="layui-icon">&#xe63c;</i>操作日志</button>
            </div>
        </script>

        <!-- 数据表格 -->
        <table class="layui-table" id="role-table" lay-filter="role-table"></table>
    </div>
</div>

<!-- 表单弹窗 -->
<script type="text/html" id="role-model">
    <form id="role-form" lay-filter="role-form" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">角色名</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入角色名" type="text" class="layui-input" maxlength="20"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item model-form-footer">
            <button class="layui-btn layui-btn-primary close" type="button">取消</button>
            <button class="layui-btn" lay-filter="role-form-submit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="role-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="auth">权限管理</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="role-tpl-status">
    <input type="checkbox" lay-filter="role-tpl-status" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|禁用" {{d.status==0?'checked':''}}/>
</script>
<link rel="stylesheet" href="/assets/libs/zTree_v3/css/metroStyle/metroStyle.css"/>
<script type="text/javascript" src="/assets/libs/zTree_v3/js/jquery.ztree.all.min.js"></script>

<script>
    layui.use(['form', 'table', 'config', 'crab'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var crab = layui.crab;
        var method = 'POST';

        //渲染表格
        var roleTable = table.render({
            elem: '#role-table',
            toolbar: '#role-toolbar',
            url: config.base_server + '/sys/role/page',
            page: true,
            parseData: config.parseData,
            cols: [[
                {type: 'numbers'},
                {field: 'name', title: '名称', width: 200},
                {field: 'remark', title: '备注'},
                {field: 'sort', title: '排序', width: 80, sort: true},
                {field: 'state', templet: '#role-tpl-status', title: '状态', width: 100},
                {field: 'createTime', title: '创建时间', sort: true, width: 150},
                {fixed: 'right', align: 'center', toolbar: '#role-table-bar', title: '操作', width: 200}
            ]]
        });

        // 表单提交事件
        form.on('submit(role-form-submit)', function (data) {
            crab.request('/sys/role', data.field, function () {
                roleTable.reload('role-table');
                layer.closeAll('page');
            }, method);
            return false;
        });

        // 表格顶部操作列
        table.on('toolbar(role-table)', function (obj) {
            if (obj.event === 'add') {
                showEditModel();
            }
        });

        // 工具条点击事件
        table.on('tool(role-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                showEditModel(data);
            } else if (obj.event === 'del') {
                //删除
                layer.confirm('确定要删除该条角色记录吗？', function () {
                    crab.deleteRow(obj, '/sys/role/' + obj.data.id);
                });
            } else if (obj.event === 'auth') {
                // 权限管理
                showPermDialog(obj.data.id);
            }
        });

        // 搜索按钮点击事件
        $('#role-btn-search').click(function () {
            roleTable.reload({where: crab.getSearchForm()});
        });

        // 显示编辑弹窗
        var showEditModel = function (data) {
            method = data ? 'PUT' : 'POST';
            layer.open({
                type: 1,
                title: data ? '修改角色' : '添加角色',
                area: '450px',
                offset: '120px',
                content: $('#role-model').html(),
                success: function () {
                    $('#role-form')[0].reset();
                    if (data) {
                        form.val('role-form', data);
                    }
                    $('#role-form .close').click(function () {
                        layer.closeAll('page');
                    });
                }
            });
        };

        // 权限管理
        var showPermDialog = function (id) {
            layer.open({
                type: 1,
                title: '权限管理',
                area: ['390px', '500px'],
                offset: '120px',
                content: '<ul id="treeAuth" class="ztree" style="padding: 25px 0px 20px 60px;"></ul>',
                btn: ['保存', '关闭'],
                btnAlign: 'c',
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                    layer.load(2);
                    var setting = {
                        check: {enable: true},
                        data: {
                            simpleData: {enable: true}
                        }
                    };
                    crab.request('/sys/role/ztree_' + id, {}, function (data) {
                        $.fn.zTree.init($('#treeAuth'), setting, data);
                        layer.closeAll('loading');
                    });
                },
                yes: function (index) {
                    var treeObj = $.fn.zTree.getZTreeObj('treeAuth');
                    var nodes = treeObj.getCheckedNodes(true);
                    var ids = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        ids[i] = nodes[i].id;
                    }
                    crab.post('/sys/role/ztree', JSON.stringify({
                        roleId: id,
                        resourceIds: ids
                    }), function () {
                        layer.msg('权限设置成功', {icon: 1});
                        layer.close(index);
                    }, true);
                }
            });
        }

        // 修改role状态
        form.on('switch(role-tpl-status)', function (obj) {
            crab.put('/sys/role/status_' + obj.elem.value, {
                status: obj.elem.checked ? 0 : -1
            }, function () {
                layer.msg('角色状态修改成功', {icon: 1});
            });
        });
    });
</script>